<template>
  <el-dialog title="编辑节点属性" :visible.sync="dialogVisible" width="60%" :before-close="closeDialog" @open="openDialog">

    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="ID" prop="id" :label-width="'120px'">
        <el-input v-model="form.id" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type" :label-width="'120px'">
        <el-input v-model="form.type" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name" :label-width="'120px'">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="故障等级" prop="level" :label-width="'120px'" style="text-align: left;">
        <el-select v-model="form.level" placeholder="请选择故障等级" clearable>
          <el-option
            v-for="level in levelOption"
            :key="level.value"
            :label="level.label"
            :value="level.value"
            ></el-option>
        </el-select>
      </el-form-item>
      <!-- 不同的 type 节点的属性要不同，后续确定每种 type 的属性后用 v-if 补充，暂时空着 -->
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="formDataUpdate">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      dialogVisible: {
        type: Boolean,
        required: true
      },
      formData: {
        type: Object,
        required: true
      }
    },
    data: function () {
      return {
        form: {...this.formData},
        levelOption: [
          { value:0 , label:'故障等级0'},
          { value:1 , label:'故障等级1'},
          { value:2 , label:'故障等级2'},
          { value:3 , label:'故障等级3'}
        ],
        rules: {
          name: [{ required: true, message: '请输入节点名称', trigger: 'blur' }],
          level: [{ required: true, message: '请选择故障等级', trigger: 'change' }],
        },
        errorInfo: ''
      }
    },
    methods: {
      formDataUpdate () {
        this.$refs['form'].validate((valid, errs) => {
          if (valid) {
            this.$emit('data-update', this.form)
            this.$emit('update:dialogVisible', false)
          } else {
            // this.errorInfo = ''
            // for (let key in errs) {
            //   this.errorInfo += errs[key][0].message + '\n'
            // }
            // this.$alert(this.errorInfo)
          }
        })
      },
      closeDialog () {
        this.$emit('update:dialogVisible', false)
      },
      openDialog () {
        this.form = {...this.formData}
      }
    }
  }
</script>